﻿#if UNITY_WECHAT_GAME
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using WeChatWASM;
namespace HBFramework
{
    /// <summary>
    /// 微信平台能力
    /// </summary>
    public class WxMiniGameManager : Singleton<WxMiniGameManager>, ISingleton
    {
        private bool initSuccess = false;

        public void OnInit()
        {
            if (Application.platform == RuntimePlatform.OSXEditor)
                return;
            if (Application.platform == RuntimePlatform.WindowsEditor)
                return;
            if (Application.platform == RuntimePlatform.LinuxEditor)
                return;
            WX.InitSDK(code =>
            {
                initSuccess = true;
            });
        }

        public void OnDestroy()
        {

        }

        /// <summary>
        /// 创建用户信息按钮
        /// </summary>
        /// <param name="rect"></param>
        /// <param name="lang"></param>
        /// <param name="withCredentials"></param>
        /// <returns></returns>
        public WXUserInfoButton CreateUserInfoButton(RectTransform rect, Action<WXUserInfoResponse> onTap,
            string lang = "zh_CN", bool withCredentials = false)
        {
            if (initSuccess) return null;

            int x = (int)rect.position.x;
            int y = (int)rect.position.y;
            int height = (int)rect.sizeDelta.x;
            int width = (int)rect.sizeDelta.y;
            var button = WX.CreateUserInfoButton(x, y, height, width, lang, withCredentials);
            button.OnTap(onTap);
            return button;
        }

        /// <summary>
        /// 获取用户信息
        /// </summary>
        public void GetUserInfo(Action<GetUserInfoSuccessCallbackResult> onSuccess, Action<GeneralCallbackResult> onFail)
        {
            if (initSuccess) return;

            var option = new GetUserInfoOption();
            option.success = onSuccess;
            option.fail = onFail;
            WX.GetUserInfo(option);
        }

        /// <summary>
        /// 获取权限
        /// </summary>
        /// <param name="scope"></param>
        /// <param name="callback"></param>
        public void GetSetting(string scope, Action<bool> callback)
        {
            if (initSuccess) return;

            var option = new GetSettingOption();
            option.success = (res) =>
            {
                bool hasScope = res.authSetting[scope];
                callback?.Invoke(hasScope);
            };
            WX.GetSetting(option);
        }

        /// <summary>
        /// 发起授权请求
        /// </summary>
        /// <param name="scope"></param>
        /// <param name="onSuccess"></param>
        /// <param name="onFail"></param>
        public void AuthorizeScope(string scope, Action<GeneralCallbackResult> onSuccess, Action<GeneralCallbackResult> onFail)
        {
            AuthorizeOption option = new AuthorizeOption();
            option.scope = scope;
            option.success = onSuccess;
            option.fail = onFail;
            WX.Authorize(option);
        }
    }
}
#endif